﻿using WeiXin.Base.Pay.Lib;

namespace WeiXin.Base.Pay
{
    /// <summary>
    /// 订单查询服务
    /// </summary>
    public class OrderQuery
    {
        /// <summary>
        /// 订单查询完整业务流程逻辑,微信订单号和商品订单号任选其一
        /// </summary>
        /// <param name="transaction_id">微信订单号（优先使用）</param>
        /// <param name="out_trade_no">商户订单号</param>
        /// <returns>订单查询结果（xml格式）</returns>
        public static WxPayData Run(string transaction_id, string out_trade_no)
        {
            #region 订单查询完整业务流程逻辑,微信订单号和商品订单号任选其一
            WxPayLog.Info("OrderQuery", "OrderQuery is processing...");

            WxPayData data = new WxPayData();
            //如果微信订单号存在，则以微信订单号为准
            if (!string.IsNullOrEmpty(transaction_id))
            {
                data.SetValue("transaction_id", transaction_id);
            }
            else//微信订单号不存在，才根据商户订单号去查单
            {
                data.SetValue("out_trade_no", out_trade_no);
            }
            //提交订单查询请求给API，接收返回数据
            WxPayData result = WxPayApi.OrderQuery(data);

            WxPayLog.Info("OrderQuery", "OrderQuery process complete, result : " + result.ToXml());
            return result; 
            #endregion
        }
    }
}